package org.linlinjava.litemall.admin.web;

import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.ProxyIntroduce;
import org.linlinjava.litemall.db.service.LitemallProxyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理服务
 *
 * @author zhy
 * @date 2019-01-14 14:11
 **/
@RestController
@RequestMapping("/admin/proxy")
public class AdminProxyController {

	private LitemallProxyService litemallProxyService;

	public AdminProxyController(LitemallProxyService litemallProxyService) {
		this.litemallProxyService = litemallProxyService;
	}

	@PostMapping("/create")
	public Object create(@LoginAdmin Integer adminId, @RequestBody ProxyIntroduce proxyIntroduce){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		litemallProxyService.add(proxyIntroduce);
		return ResponseUtil.ok();
	}

	@GetMapping("/list")
	public Object list(@LoginAdmin Integer adminId,
		String id, String name,
		@RequestParam(value = "page", defaultValue = "1") Integer page,
		@RequestParam(value = "limit", defaultValue = "10") Integer limit,
		String sort, String order){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}

		List<ProxyIntroduce> categoryList = litemallProxyService.querySelective( name, page, limit);
		long total = PageInfo.of(categoryList).getTotal();

		Map<String, Object> data = new HashMap<>(16);
		data.put("total", total);
		data.put("items", categoryList);
		return ResponseUtil.ok(data);
	}

	@PostMapping("/delete")
	public Object delete(@LoginAdmin Integer adminId, @RequestBody ProxyIntroduce proxyIntroduce){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		litemallProxyService.deletedById(proxyIntroduce.getId());
		return ResponseUtil.ok();
	}

	@PostMapping("/update")
	public Object update(@LoginAdmin Integer adminId, @RequestBody ProxyIntroduce proxyIntroduce){
		if(adminId == null){
			return ResponseUtil.unlogin();
		}
		litemallProxyService.updateById(proxyIntroduce);
		return ResponseUtil.ok();
	}

}
